﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Media;
using System.Windows.Forms;
using AForge;
using AForge.Math.Geometry;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Vision;
using System.Reflection;
using System.Drawing.Imaging;

namespace nxt
{
    class ImageProcessor
    {
        //Private
       AForge.Point _TargetCenter;
        
        //Events
        public delegate void NewTargetPositionHandler(IntPoint Center, Bitmap image);
        public event NewTargetPositionHandler NewTargetPosition;

        public ImageProcessor()
        {
            _TargetCenter = new AForge.Point(0, 0);
        }

        public void Goal_tracking(Bitmap bitmap)
        {

            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
            Bitmap uimage = bitmap;
            HSLFiltering cf = new HSLFiltering();
            cf.Hue = new IntRange(340, 20);
            cf.Saturation = new Range(0.6f, 1);
            cf.Luminance = new Range(0.1f, 1);
            /*ColorFiltering cf = new ColorFiltering();
            cf.Red = new IntRange(170, 255);
            cf.Green = new IntRange(50, 150);
            cf.Blue = new IntRange(0, 80);*/
            cf.ApplyInPlace(bitmapData);
            bitmap.UnlockBits(bitmapData);
            bitmap = Grayscale.CommonAlgorithms.RMY.Apply(bitmap);
            Threshold filterThreshold = new Threshold(180);
            filterThreshold.ApplyInPlace(bitmap);
            // a megmaradt foltok behatározása
            BlobCounter bc = new BlobCounter();
            bc.FilterBlobs = true;
            bc.MinHeight = 5;
            bc.MinWidth = 5;
            bc.ProcessImage(bitmapData);
            Blob[] blobs = bc.GetObjectsInformation();
            //bitmap.UnlockBits(bitmapData);

            SimpleShapeChecker sc = new SimpleShapeChecker();
            Pen penRect = new Pen(Color.Blue, 3);
            Pen penLine = new Pen(Color.Blue, 3);
            IntPoint BigestCenter = new IntPoint(0, 0);
            Graphics g = Graphics.FromImage(uimage);

            if (blobs.Length > 0)
            {
                Blob BigestBlob = blobs[0];
                foreach (Blob blob in blobs)
                {
                    if (blob.Area > BigestBlob.Area)
                    {
                        BigestBlob = blob;
                    }
                }
                List<IntPoint> edgePoints = bc.GetBlobsEdgePoints(BigestBlob);

                BigestCenter = (IntPoint)BigestBlob.CenterOfGravity;
                g.DrawRectangle(penRect, BigestBlob.Rectangle);
                g.DrawLine(penLine, BigestBlob.CenterOfGravity.X, 0, BigestBlob.CenterOfGravity.X, 240);
                g.DrawLine(penLine, 0, BigestBlob.CenterOfGravity.Y, 320, BigestBlob.CenterOfGravity.Y);
            }

            g.Dispose();
            NewTargetPosition(BigestCenter, uimage);
        }


        public void Ball_tracking(Bitmap bitmap)
        {

            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
            Bitmap uimage = bitmap;
            HSLFiltering cf = new HSLFiltering();
            cf.Hue = new IntRange(340, 20);
            cf.Saturation = new Range(0.6f, 1);
            cf.Luminance = new Range(0.1f, 1);
            cf.ApplyInPlace(bitmapData);
            bitmap.UnlockBits(bitmapData);
            bitmap = Grayscale.CommonAlgorithms.RMY.Apply(bitmap);
            Threshold filterThreshold = new Threshold(180);
            filterThreshold.ApplyInPlace(bitmap);
            // a megmaradt foltok behatározása
            BlobCounter bc = new BlobCounter();
            bc.FilterBlobs = true;
            bc.MinHeight = 5;
            bc.MinWidth = 5;
            bc.ProcessImage(bitmapData);
            Blob[] blobs = bc.GetObjectsInformation();
            //bitmap.UnlockBits(bitmapData);

            SimpleShapeChecker sc = new SimpleShapeChecker();
            Pen penRect = new Pen(Color.Blue, 3);
            Pen penLine = new Pen(Color.Blue, 3);
            IntPoint BigestCenter = new IntPoint(0, 0);
            Graphics g = Graphics.FromImage(uimage);

            if (blobs.Length > 0)
            {
                Blob BigestBlob = blobs[0];
                foreach (Blob blob in blobs)
                {
                    if (blob.Area > BigestBlob.Area)
                    {
                        BigestBlob = blob;
                    }
                }
                List<IntPoint> edgePoints = bc.GetBlobsEdgePoints(BigestBlob);

                BigestCenter = (IntPoint)BigestBlob.CenterOfGravity;
                g.DrawRectangle(penRect, BigestBlob.Rectangle);
                g.DrawLine(penLine, BigestBlob.CenterOfGravity.X, 0, BigestBlob.CenterOfGravity.X, 240);
                g.DrawLine(penLine, 0, BigestBlob.CenterOfGravity.Y, 320, BigestBlob.CenterOfGravity.Y);
            }

            g.Dispose();
            NewTargetPosition(BigestCenter, uimage);
        }

        /*public void Process(UnmanagedImage uimage)
        {


            Bitmap image = uimage.ToManagedImage();
            ColorFiltering filter = new ColorFiltering();
            filter.Red = new IntRange(170, 255);
            filter.Green = new IntRange(0, 150);
            filter.Blue = new IntRange(0, 190);

            filter.ApplyInPlace(uimage);
            //filter.FillOutsideRange = true;

            // 2 - grayscale image
            //uimage = Grayscale.CommonAlgorithms.BT709.Apply(uimage);
            uimage = Grayscale.CommonAlgorithms.RMY.Apply(uimage);

            // 3 - treshold
            Threshold filterThreshold = new Threshold(180);
            //OtsuThreshold filterThreshold = new OtsuThreshold();
            filterThreshold.ApplyInPlace(uimage);

            // 4 - Blob counting
            BlobCounter blobCounter = new BlobCounter();
            blobCounter.FilterBlobs = true;
            blobCounter.MinWidth = 3;
            blobCounter.MinWidth = 3;
            blobCounter.MaxWidth = 320;
            blobCounter.MaxHeight = 100;

            blobCounter.ProcessImage(uimage);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            SimpleShapeChecker sc = new SimpleShapeChecker();

            Graphics g = Graphics.FromImage(image);
            Pen penRect = new Pen(Color.Blue, 3);
            Pen penLine = new Pen(Color.Blue, 3);
            IntPoint BigestCenter = new IntPoint(0, 0);

            if (blobs.Length > 0)
            {
                Blob BigestBlob = blobs[0];
                foreach (Blob blob in blobs)
                {
                    if (blob.Area > BigestBlob.Area)
                    {
                        BigestBlob = blob;
                    }
                }

                BigestCenter = (IntPoint)BigestBlob.CenterOfGravity;
                g.DrawRectangle(penRect, BigestBlob.Rectangle);
                g.DrawLine(penLine, BigestBlob.CenterOfGravity.X, 0, BigestBlob.CenterOfGravity.X, image.Height);
                g.DrawLine(penLine, 0, BigestBlob.CenterOfGravity.Y, image.Width, BigestBlob.CenterOfGravity.Y);
            }

            NewTargetPosition(BigestCenter, image);

            g.Dispose();
            penRect.Dispose();
            penLine.Dispose();
        }*/




    }
}